/**
 * Javascript file for notices.php
 */

var on_notice_save_functions = [];
var on_notice_save_function_names = new Array();


function count_character() {
	var string_length = dijit.byId("textcontent").get("value").length;
	dojo.byId("cntChars").innerHTML = string_length;
}

function reset() {
	dojo.byId("notice_edit").reset();
	count_character();
}

function save_notice() {
	var xhrArgs = {
	        url: "ajax/ajax_notices.php?type=update",
	        form: dojo.byId("notice_edit"),
	        handleAs: "json",
	        load: function(jsonData) {
	        	showMessages(jsonData);
	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);	
	return false; // to avoid form submit
}

function update_notice(dijitDialog, dijitContentPane, playerid) {
	var xhrArgs = {
	        url: "ajax/ajax_notices.php?type=update",
	        form: dojo.byId("notice_edit"),
	        handleAs: "json",
	        load: function(jsonData) {
	        	showMessages(jsonData);
	        	
	        	dojo.forEach(on_notice_save_functions, function(entry, i){
	        	  console.debug(entry, "at index", i);
	        	});
	        	while (on_notice_save_function_names.length > 0) {
	        		var function_name = on_notice_save_function_names.pop();
	        		on_notice_save_functions[function_name](dijitContentPane, playerid);
	        	}
       	
	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);	
	dijit.byId(dijitDialog).hide();
			
	return false; // to avoid form submit
}


function galaxytool_notices_init() {
	try {
		count_character();
	} catch(ignore_exception) {}
}

dojo.ready(galaxytool_notices_init);

on_notice_save_functions['populate_notice'] = function (dijitContentPane, playerid) { 
	if (dijitContentPane != "")	dijit.byId(dijitContentPane).set("href","ajax/ajax_notices_read.php?playerid="+playerid);
};
on_notice_save_function_names.push('populate_notice');
